<?php

/**
 * Description of LoginController
 *
 * @author gun
 */
require_once 'BaseController.php';
class Admin_LoginController extends Admin_BaseController {    
    
    public function init() {
        $this->_helper->layout->setLayout('login');
        
    }
    
    public function indexAction() {
        
    }
    
    public function loginVerifyAction() {
        $name = $this->getParam('username');
        $password = $this->getParam('password');
        
        $hashp = md5($password);
        
        $where = array('name = ?' => $name, 'password = ?' => $hashp);
        
        $service = AppRegistry::loadService('Admin_Model_UserService');
        $data = $service->getRow($where);
        
        if ($data) {            
            $session = new Zend_Session_Namespace(Admin_SecureController::$_NS);
            $session->user = new stdClass();
            $session->user->name = $name;
            $session->user->level = $data['level'];
            
            $where2 = array('a.groupID = ?' => $session->user->level, 'a.status = ?' => true);

            $menuService = AppRegistry::loadService('Admin_Model_MenuService');
            $url = $menuService->findAll($where2);    
            
            foreach($url as $value)
                if ('resource/index' === strtolower($value['link'])) 
                    $url[]['link'] = 'resource-parameter/index';
            
            $specialRoles = array('change-password'=>'process-ch-password');
            
            foreach($url as $value) {
                $link = ($value['link'] ? $value['link'] : '##/###');
                $session->user->roles[] = $link;                                
                
                $explod = explode('/', $link);
                $controller = strtolower($explod[0]); 
                $function = strtolower($explod[1]);
                
                if ('index' === $function) {
                    $additionalRole = $this->getMethod($controller);
                    
                    foreach ($additionalRole as $key => $val) {
                        $menu = $controller . "/" . $val;
                        if ($menu !== $link)
                            $session->user->roles[] = $controller . "/" . $val;
                    }
                    
                } else if (key_exists($function, $specialRoles)) {
                    $session->user->roles[] = $controller . "/" . $specialRoles[$function];
                }
            }
//            print_r($session->user->roles); exit;
            $session->user->total_roles = sizeof($session->user->roles);
            $this->redirect('admin/index');
            
        } else {
            $this->redirect('admin/login/index');
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function getMethod($controller) {        
        $actions = array();
        $camelCase = new Zend_Filter_Word_DashToCamelCase();
        $name = $camelCase->filter($controller);
                
        $module = $this->getRequest()->getModuleName();
        $path = $this->getFrontController()->getControllerDirectory($module);
        $file = $name . 'Controller.php';
        $class = ucfirst($module) . "_" . $name . 'Controller';

        if (file_exists($path . DIRECTORY_SEPARATOR . $file)) {
            $filter = new Zend_Filter_Word_CamelCaseToDash();

            include_once ($path . DIRECTORY_SEPARATOR . $file);
            if (class_exists($class, TRUE)) {
                foreach(get_class_methods($class) as $action) {
                    if (FALSE !== strpos($action, 'Action')) {
                        $actions[] = strtolower($filter->filter(substr($action, 0, -6)));
                    }
                }
            }
        }
        
        return $actions;
    }
    
    public function logoutAction() {
        Zend_Session::destroy();
        $this->redirect('doc/index');
    }
}

?>
